package com.kmzx.gen.signs.events;

import com.kmzx.gen.signs.dao.GenDataMapper;
import com.kmzx.gen.signs.entity.GenData;
import com.kmzx.gen.signs.utils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Project gen_sign
 * @Author: carol
 * @Date: 2020/7/8-10:08
 */

@Component(value = "beSignEventListener")
public class BeSignEventListener {

    private static Logger logger = LoggerFactory.getLogger(JRedisMessageListener.class);


    @Value("${remote.url}")
    private String remoteUrl;

    @Autowired
    GenDataMapper genDataMapper;

    @Async
    @EventListener(classes = {BeSignEvent.class})
    public void defaultBeSignEvent(BeSignEvent beSignEvent) throws Exception {
        String signId = beSignEvent.getSignId();
        GenData genData = genDataMapper.selectById(signId);

        String dataId = genData.getDataId();
        String signedData = genData.getSignedData();
        String sessionId = genData.getSessionId();

        logger.info("数据为:{}",genData);
        Integer finished = genData.getFinished();
        if (finished == 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", dataId);
            map.put("data", signedData);
            map.put("sessionId", sessionId);

            String res = HttpClientUtil.postJsonBody(remoteUrl, map);
            logger.info("上报数据结果:{}",res);
        } else {
            logger.info("数据已经处理完成");
        }

    }

}
